package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.service.IDrugDiseaseService;
import com.ruoyi.web.service.IDrugTdkService;
import com.ruoyi.web.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.DrugMapper;
import com.ruoyi.web.service.IDrugService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 药品（新）Service业务层处理
 * 
 * @author drx
 * @date 2024-03-11
 */
@Service
public class DrugServiceImpl implements IDrugService 
{
    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private IDrugDiseaseService drugDiseaseService;

    @Autowired
    private IDrugTdkService drugTdkService;

    @Autowired
    private INewsService newsService;



    /**
     * 查询药品（新）
     * 
     * @param id 药品（新）主键
     * @return 药品（新）
     */
    @Override
    public Drug selectDrugById(Long id)
    {
        DrugDisease drugDisease = new DrugDisease();
        drugDisease.setDrugId(id);
        List<DrugDisease> list = drugDiseaseService.selectDrugDiseaseList(drugDisease);
        Drug drug = drugMapper.selectDrugById(id);
        drug.setDiseaseList(list);
        DrugTdk drugTdk = drugTdkService.selectDrugTdkByDrugId(id);
        drug.setDrugTdk(drugTdk);
        return drug;
    }

    /**
     * 前端查询详情用
     *
     * @param id oldDrug主键
     * @return oldDrug
     */
    @Override
    public AjaxResult getDrugById(Long id)
    {
        AjaxResult ajaxResult=new AjaxResult();
        try{
            Drug drug = drugMapper.selectDrugById(id);
            if(null != drug){
                //状态为未启用时，返回空对象
                if("0".equals(drug.getState())){
                    ajaxResult.put("data",null);
                    ajaxResult.put("code","1");
                    ajaxResult.put("msg","查询成功！");
                    return  ajaxResult;
                }
                //获取疾病信息
                DrugDisease drugDisease = new DrugDisease();
                drugDisease.setDrugId(id);
                List<DrugDisease> list = drugDiseaseService.selectDrugDiseaseList(drugDisease);
                drug.setDiseaseList(list);
                //获取其他tdk信息
                DrugTdk drugTdk = drugTdkService.selectDrugTdkByDrugId(id);
                drug.setDrugTdk(drugTdk);
            }
            ajaxResult.put("data",drug);
            ajaxResult.put("code","1");
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取药品详情失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /**
     * 查询药品（新）列表
     * 
     * @param drug 药品（新）
     * @return 药品（新）
     */
    @Override
    public List<Drug> selectDrugList(Drug drug)
    {
        return drugMapper.selectDrugList(drug);
    }

    /**
     * 新增药品（新）
     * 
     * @param drug 药品（新）
     * @return 结果
     */
    @Override
    public int insertDrug(Drug drug)
    {
        drug.setCreateTime(DateUtils.getNowDate());
        drug.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        drug.setCreateName(ShiroUtils.getSysUser().getUserName());
        drug.setUpdateTime(DateUtils.getNowDate());
        drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        //保存药品
        int rows = drugMapper.insertDrug(drug);
        //保存疾病信息
        List<DrugDisease> list = drug.getDiseaseList();
        if(list.size()>0){
            list.forEach(item->{
                DrugDisease drugDisease = new DrugDisease();
                drugDisease.setDiseaseId(item.getDiseaseId());
                drugDisease.setPid(item.getPid());
                drugDisease.setDrugId(drug.getId());
                drugDiseaseService.insertDrugDisease(drugDisease);
            });
        }
        //保存其他tdk信息
        DrugTdk drugTdk = drug.getDrugTdk();
        drugTdk.setDrugId(drug.getId());
        drugTdkService.insertDrugTdk(drugTdk);
        return rows;
    }

    /**
     * 修改药品（新）
     * 
     * @param drug 药品（新）
     * @return 结果
     */

    @Override
    public int updateDrug(Drug drug)
    {
        drug.setUpdateTime(DateUtils.getNowDate());
        drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        //修改药品
        int rows = drugMapper.updateDrug(drug);
        //根据药品id删除疾病
        drugDiseaseService.deleteDrugDiseaseByDrugId(drug.getId());
        //保存疾病信息
        List<DrugDisease> list = drug.getDiseaseList();
        if(list.size()>0){
            list.forEach(item->{
                DrugDisease drugDisease = new DrugDisease();
                drugDisease.setDiseaseId(item.getDiseaseId());
                drugDisease.setPid(item.getPid());
                drugDisease.setDrugId(drug.getId());
                drugDiseaseService.insertDrugDisease(drugDisease);
            });
        }
        DrugTdk drugTdk = drug.getDrugTdk();

        if(null != drugTdk.getId()){
            //保存其他tdk信息
            drugTdkService.updateDrugTdk(drug.getDrugTdk());
        }else{
            //保存其他tdk信息
            drugTdk.setDrugId(drug.getId());
            drugTdkService.insertDrugTdk(drugTdk);
        }
        return rows;
    }

    /**
     * 修改药品（新）
     *
     * @param drug 药品（新）
     * @return 结果
     */
    @Override
    public int updateState(Drug drug)
    {
        drug.setUpdateTime(DateUtils.getNowDate());
        drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        //修改药品
        int rows = drugMapper.updateDrug(drug);

        return rows;
    }

    /**
     * 修改药品（新）
     *
     * @param drug 药品（新）
     * @return 结果
     */
    @Override
    public int editRecommend(Drug drug)
    {
        drug.setUpdateTime(DateUtils.getNowDate());
        drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        //修改药品
        int rows = drugMapper.updateDrug(drug);

        return rows;
    }


    /**
     * 批量删除药品（新）
     * 
     * @param ids 需要删除的药品（新）主键
     * @return 结果
     */
    @Override
    public int deleteDrugByIds(String ids)
    {
        return drugMapper.deleteDrugByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除药品（新）信息
     * 
     * @param id 药品（新）主键
     * @return 结果
     */
    @Override
    public int deleteDrugById(Long id)
    {
        return drugMapper.deleteDrugById(id,ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 修改drug发布状态
     *
     * @param drug
     * @return 结果
     */
    @Transactional
    public int updateDrugState(Drug drug)
    {
        drug.setUpdateTime(DateUtils.getNowDate());
        drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return drugMapper.updateDrug(drug);
    }

    /**
     * 获取首页药品目录视图
     *
     * @return List<Drug>
     */
    public AjaxResult selectDrugListBySv(){
        AjaxResult ajaxResult=new AjaxResult();
        try{
            List<Drug> list = drugMapper.selectDrugListBySv();
            ajaxResult.put("data",list);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取首页热门药品失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /**
     * 获取首页药品目录视图
     *
     * @return List<Drug>
     */
    public AjaxResult getDrugListView(){
        AjaxResult ajaxResult=new AjaxResult();
        try{
            List<Drug> list = drugMapper.getDrugListView();
            ajaxResult.put("data",list);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取首页药品目录失败，请联系管理员！");
        }
        return ajaxResult;
    }

    public AjaxResult getDrugListTopFive(){
        AjaxResult ajaxResult=new AjaxResult();
        try{
            //获取视图中前五名最新药品
            List<Drug> list = drugMapper.getDrugListTopFive();
            //遍历药品 每个药品返回4个新闻
            for (Drug drug : list) {
                News news = new News();
                news.setDrugId(drug.getId());
                news.setState("1");
                news.setSize(4);
                List<News> newsList = newsService.selectNewsList(news);
                drug.setNewsList(newsList);
            }
            ajaxResult.put("data",list);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取首页药品目录失败，请联系管理员！");
        }
        return ajaxResult;
    }

    public AjaxResult getDrugRecommend(){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            List<Drug> newsDrugRecommend = drugMapper.getDrugRecommend();
            ajaxResult.put("data",newsDrugRecommend);
            ajaxResult.put("code", Constants.SUCCESS);
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",Constants.FAIL);
            ajaxResult.put("msg","获取首页推荐药品目录失败，请联系管理员！");
        }
        return ajaxResult;
    }
}
